package com.hl.payServer.core.wePay;


import com.hl.payServer.model.enums.WepayField;
import com.hl.payServer.util.Preconditions;

import java.util.Map;
import java.util.TreeMap;


/**
 * 对帐单组件
 */
public final class OnBill extends Component {

    /**
     * 下载对帐单
     */
    private static final String downloadbill = "https://api.mch.weixin.qq.com/pay/downloadbill";

    OnBill(Wepay wepay) {
        super(wepay);
    }

    /**
     * 下载对帐单
     * @param request 下载对帐单对象
     * @return RefundResponse对象，或抛WepayException
     */
    public String download(OnBillRequest request){
        checkApplyParams(request);
        Map<String, String> applyParams = buildApplyParams(request);
        return doPostObBill(downloadbill,applyParams);
    }

    /**
     * 校验对帐单参数
     * @param request 对帐单对象
     */
    private void checkApplyParams(OnBillRequest request) {
        Preconditions.checkNotNullAndEmpty(request.getNonceStr(), "随机字符串不能为空");
        Preconditions.checkNotNullAndEmpty(request.getBillDate(), "对账单日期不能为空");
        Preconditions.checkNotNullAndEmpty(request.getBillType(), "账单类型不能为空");
    }

    /**
     * 构建对帐单参数
     * @param request 对帐单请求
     * @return 对帐单参数
     */
    private Map<String, String> buildApplyParams(OnBillRequest request) {
        Map<String, String> params = new TreeMap<String, String>();
        // 配置参数
        buildConfigParams(params);
        // 业务参数
        putIfNotEmpty(params, WepayField.NONCE_STR, request.getNonceStr());
        putIfNotEmpty(params, WepayField.BILL_DATE, request.getBillDate());
        putIfNotEmpty(params, WepayField.BILL_TYPE, request.getBillType());
        // 签名参数
        buildSignParams(params);
        return params;
    }
}
